package cn.zcy.utils;

import cn.zcy.domain.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 记录日志的工具类
 */
@Component
@Aspect //表明当前类是一个切面类(切面)
@Order(2)
public class LogUtil {

    /**
     * 通用切入点表达式
     */
    @Pointcut(value = "execution(public * cn.zcy.service.impl.*.*(..)) && args(user)") //argNames = "user"（不是必要的） ->参考 && args(user)
    private void pointCut(User user){}
    /**
     * 用于配置当前方法是一个前置通知
     */
//    @Before(value = "pointCut(user)",argNames = "user")//对切入点表达式的引用
    @Before(value = "execution(public * cn.zcy.service.impl.*.*(..))")//对切入点表达式的引用
    public void before() {
        System.out.println("前置增强");
    }

    @After(value = "execution(public * *..*.*(..))")//对切入点表达式的引用
    public void after() {
        System.out.println("后置增强");
    }


    @AfterReturning(value = "execution(public * *..*.*(..))")//对切入点表达式的引用 ,returning = "resoltValue"
    public void afterReturning(JoinPoint joinPoint ) {//,Object resoltValue
//        String name = joinPoint.getSignature().getName();
//        System.out.println("方法的名称 = " + name);
//        Object[] args = joinPoint.getArgs();
//        for (Object arg : args) {
//            System.out.println("参数 = " + arg);
//        }

        System.out.println("后置返回增强");
    }

    @AfterThrowing(value = "execution(public * *..*.*(..))",throwing = "e")//对切入点表达式的引用
    public void afterThrowing(Throwable e) {
        System.out.println("异常增强"+e);
    }
}
